const util = require('../../utils/util.js');
const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');

var app = getApp();

Page({
  data: {
    alarms: [{}, {}], //报警数据
    navData: [],
    date: '2020-08-01',
    currentTab: 0,
    navScrollLeft: 0,
    pixelRatio: 0,
    windowHeight: 0,
    windowWidth: 0

  },

  onLoad: function (options) {
    let that = this
    var dates = []
    for (var i = -30; i < 0; i++) {
      dates.push({
        'date': this.getDateStr(i)
      })
    }
    this.setData({
      navData: dates
    })

    this.data.navData.forEach(function (item, index) {
      let keyParam = "navData[" + index + "].key"
      that.setData({
        [keyParam]: item.date.split("-")[1] + '月' + item.date.split("-")[2] + '日'
      })
    })



    this.setData({
      currentTab: this.data.navData.length - 1,
      date: this.data.navData[29].date
    })

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          pixelRatio: res.pixelRatio,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab选项居中                            
        this.setData({
          navScrollLeft: (this.data.currentTab - 2) * singleNavWidth
        })
      },
    })

    this.getHistoryAlarmList()

  },

  onShow: function () {

  },

  getDateStr: function (AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
    var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
    return y + "-" + m + "-" + d;
  },

  async switchNav(event) {
    var current = event.currentTarget.dataset.current;
    console.log(this.data.navData[current].date)
    var cur = event.currentTarget.dataset.current;
    //每个tab选项宽度占1/5
    var singleNavWidth = this.data.windowWidth / 5;
    //tab选项居中                            
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth
    })
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur,
        date: this.data.navData[cur].date
      })
    }
    this.getHistoryAlarmList()

  },

  listViewTap: function (e) {
    this.setData({
      selelctDevice: e.currentTarget.dataset.a,
      isShowList: false
    })
    this.data.deviceTypes.forEach(function (item, inndex) {
      if (this.data.selelctDevice.terminalKindID == item.id) {
        this.setData({
          selelctType: item
        })
      }

    })
  },


  //实时报警提醒
  getHistoryAlarmList: function () {
    let data = {
      date: this.data.date,
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.getHistoryAlarmList + wx.getStorageSync('id'),
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null) {
            that.setData({
              alarms: res.data,
            })
          }
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

})